package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.feign.clients.ItemClient;
import com.hmall.feign.clients.UserClient;
import com.hmall.order.common.BaseContext;
import com.hmall.order.dto.OrderRequest;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IOrderLogisticsService orderLogisticsService;

    @Override
    public Long createOrderAbout(OrderRequest params) {
        SnowflakeIdGenerator.instance.init(100);
        Long nextId = SnowflakeIdGenerator.instance.nextId();

        Item item = itemClient.getItemById(params.getItemId());

        BigDecimal totalFee = BigDecimal.valueOf(0l);

        Integer num = params.getNum();

        totalFee = new BigDecimal(num.toString()).multiply(new BigDecimal(item.getPrice().toString()));

        System.out.println("totalFee = " + totalFee);

        Order order = new Order();
        order.setId(nextId);
        order.setTotalFee(totalFee.longValue());
        order.setPaymentType(params.getPaymentType());
        order.setStatus(1);

        order.setUserId(BaseContext.getCurrentId());
        order.setCreateTime(Date.from(Instant.now()));
        order.setUpdateTime(Date.from(Instant.now()));
        this.save(order);

        OrderDetail orderDetail = new OrderDetail();

        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(params.getItemId());
        orderDetail.setNum(params.getNum());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setTitle(item.getName());

        orderDetail.setCreateTime(Calendar.getInstance().getTime());
        orderDetail.setUpdateTime(Calendar.getInstance().getTime());

        orderDetailService.save(orderDetail);

        Address address = userClient.findAddressById(params.getAddressId());
        OrderLogistics logistics = new OrderLogistics();
        logistics.setOrderId(nextId);
        logistics.setLogisticsNumber(IdWorker.getIdStr().substring(0,11));
        logistics.setLogisticsCompany("晶冬物流");
        logistics.setContact(address.getContact());
        logistics.setPhone(address.getMobile());
        logistics.setProvince(address.getProvince());
        logistics.setCity(address.getCity());
        logistics.setTown(address.getTown());
        logistics.setStreet(address.getStreet());
        logistics.setCreateTime(Calendar.getInstance().getTime());
        logistics.setUpdateTime(Calendar.getInstance().getTime());

        orderLogisticsService.save(logistics);

        itemClient.useStockData(item.getId(),params.getNum());

        return nextId;
    }
}
